package com.anolesoft.amljmes.fortysix.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.JSONObject;
import com.anolesoft.amljmes.fortysix.dto.*;
import com.anolesoft.amljmes.fortysix.service.impl.MesBzzBindServiceImpl;
import com.anolesoft.amljmes.fortysix.service.impl.MesBzzCpptmxServiceImpl;
import com.anolesoft.amljmes.fortysix.service.impl.MesBzzFollowServiceImpl;
import com.anolesoft.amljmes.fortysix.service.impl.MesBzzQtfxServiceImpl;
import com.anolesoft.amljmes.fortysix.service.mapper.MesBzzCpptmxServiceMapper;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.util.Constants;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("connector")
public class QjwlConnectorController {
    private final String URL = "http://192.168.8.106:8080/services/erpWebService?wsdl";
//    private final String URL = "http://192.168.114.10:8080/services/erpWebService?wsdl";
//    private final String URL = "http://172.17.62.246:8082/services/erpWebService?wsdl";

    @PostMapping(value="/query",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult QjwlSelectAll(MesBzzPtdmxDto dto) {

        String jx = dto.getJixing();
        String th = dto.getPartDrawingNo();
//        String pgh = dto.getPaigonghao();
        String jfcj = dto.getJiaofuchejian();
        String jscj = dto.getJieshouchejian();
        String jfsx = dto.getJiaofushunxu();
        String cdh = dto.getFactoryCode();

        try{
//            String URL = "http://192.168.8.119:8080/services/erpWebService?wsdl";
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("jx",jx);
            jsonObject.put("th",th);
//            jsonObject.put("pgh",pgh);
            jsonObject.put("cdh",cdh);
            jsonObject.put("jfcj",jfcj);
            jsonObject.put("jscj",jscj);
            jsonObject.put("jfsx",jfsx);

            Client client = dcf.createClient(URL);
            Object[] objects = new Object[0];
            objects = client.invoke("getQjwlKc",jsonObject.toString());
            Object arrStr = objects[0];
//            JSON字符串转换成实体类对象
            String jsonObjectStr = arrStr.toString();
            List<MesBzzQjwlDto> list = JSON.parseObject(jsonObjectStr, new TypeReference<ArrayList<MesBzzQjwlDto>>(){});
//            List<MesBzzQjwlDto> list=JSONArray.parseArray(jsonObjectStr,MesBzzQjwlDto.class);
            System.out.println("jsonObject-----" + list);
            AjaxResult success = AjaxResult.success();
            success.put("data", list);
            return success;
        }catch (Exception e){
            e.printStackTrace();
            AjaxResult success = AjaxResult.success();
            success.put("data", "失败");
            return success;
        }
    }


    @Autowired
    MesBzzFollowServiceImpl mesBzzFollowService;
    @Autowired
    MesBzzBindServiceImpl mesBzzBindService;
    @Autowired
    MesBzzCpptmxServiceImpl mesBzzCpptmxService;
    @Autowired
    MesBzzCpptmxServiceMapper mesBzzCpptmxServiceMapper;
    @Autowired
    MesBzzQtfxServiceImpl mesBzzQtfxService;

    @Transactional
    @PostMapping(value="/updatePt", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updatePt(@RequestBody List<PeiTaoVo> list){
        System.out.println("map>>>>>>"+list);
        PeiTaoVo map=list.get(0);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MesBzzFollowDto inFoDtoParam = new MesBzzFollowDto();
        inFoDtoParam.setPtdmxObj(map.getPtdmxObj());
        inFoDtoParam.setPaigonghao(map.getPaigonghao());
        inFoDtoParam.setJixing(map.getJixing());
        inFoDtoParam.setTuhao(map.getTuhao());
        inFoDtoParam.setJiaofuchejian(map.getJiaofuchejian());
        inFoDtoParam.setJieshouchejian(map.getJieshouchejian());
        inFoDtoParam.setJiaofushunxu(map.getJiaofushunxu());
        inFoDtoParam.setDingdanhao(map.getDingdanhao());
        inFoDtoParam.setPeitaodanhao(map.getPeitaodanhao());
        inFoDtoParam.setShuliang(map.getShulianng());
        inFoDtoParam.setFactoryId(map.getFactoryId());
        inFoDtoParam.setHegebiaoqian(map.getHegebiaoqian());
        inFoDtoParam.setCaozuoren(loginUser.getEmpUid());
        inFoDtoParam.setType(map.getType());
        inFoDtoParam.setFathername(map.getFathername());
        inFoDtoParam.setFathernumber(map.getFathernumber());
        inFoDtoParam.setPici(map.getPici());
        inFoDtoParam.setJiaci(map.getJiaci());
        inFoDtoParam.setProportion(map.getProportion());
        mesBzzFollowService.addMesBzzFollow(inFoDtoParam);
        MesBzzBindDto inFoDtoParam1 = new MesBzzBindDto();
        for(int i = 0; i < list.size(); i++){
        inFoDtoParam1.setPtdObj(list.get(i).getPtdObj());
        inFoDtoParam1.setPtdmxObj(list.get(i).getPtdmxObj());
        inFoDtoParam1.setPaigonghao(list.get(i).getPaigonghao());
        inFoDtoParam1.setJixing(list.get(i).getJixing());
        inFoDtoParam1.setTuhao(list.get(i).getTuhao());
        inFoDtoParam1.setJiaofuchejian(list.get(i).getJiaofuchejian());
        inFoDtoParam1.setJieshouchejian(list.get(i).getJieshouchejian());
        inFoDtoParam1.setJiaofushunxu(list.get(i).getJiaofushunxu());
        inFoDtoParam1.setPeitaodanhao(list.get(i).getPeitaodanhao());
        inFoDtoParam1.setFactoryId(list.get(i).getFactoryId());
        inFoDtoParam1.setHegebiaoqian(list.get(i).getHegebiaoqian());
        inFoDtoParam1.setDingdanbianhao(list.get(i).getDingdanhao());
        inFoDtoParam1.setBangdingren(loginUser.getEmpUid());
        inFoDtoParam1.setBangdingnum(list.get(i).getBangdingnum());
        inFoDtoParam1.setKucunObj(list.get(i).getKucunObj());
        inFoDtoParam1.setDanwei(list.get(i).getDanwei());
        inFoDtoParam1.setDanjishu(list.get(i).getDanjishu());
        inFoDtoParam1.setPici(list.get(i).getPici());
        inFoDtoParam1.setJiaci(list.get(i).getJiaci());

        if (list.get(i).getBangdingnum()!=null) {
            mesBzzBindService.addMesBzzBind(inFoDtoParam1);// 需要循环添加
        }
        }
        String objuid = map.getObjuid();
        int num = 0;
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getBangdingnum()!=null){
                num+=list.get(i).getBangdingnum();
            }
        }
        mesBzzCpptmxService.updateCpptmxPt(objuid,num);// 需要求和
        int n = mesBzzCpptmxServiceMapper.queryMesBzzCpptmxNum(map.getPtdObj());
        if (n == 0) {
            mesBzzQtfxService.updateIsItComplete(map.getPtdObj());
        }
        JSONObject jsonObject = new JSONObject();// 需要把库存和数量数组传过去
        jsonObject.put("list",list);
        try{
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

            Client client = dcf.createClient(URL);
            Object[] objects = new Object[0];
            objects = client.invoke("getKyslPt",jsonObject.toString());
            Object arrStr = objects[0];
            String jsonObjectStr = arrStr.toString();

            AjaxResult success = AjaxResult.success();
            success.put("data", jsonObjectStr);
            return success;
        }catch (Exception e){
            e.printStackTrace();
            AjaxResult success = AjaxResult.success();
            success.put("data", "失败");
            return success;
        }
    }

    @Transactional
    @PostMapping(value="/updateJc", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateJc(@RequestBody List<PeiTaoVo> list){
        System.out.println("map>>>>>>"+list);
        PeiTaoVo map=list.get(0);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MesBzzFollowDto inFoDtoParam = new MesBzzFollowDto();
        inFoDtoParam.setPtdmxObj(map.getPtdmxObj());
        inFoDtoParam.setPaigonghao(map.getPaigonghao());
        inFoDtoParam.setJixing(map.getJixing());
        inFoDtoParam.setTuhao(map.getTuhao());
        inFoDtoParam.setJiaofuchejian(map.getJiaofuchejian());
        inFoDtoParam.setJieshouchejian(map.getJieshouchejian());
        inFoDtoParam.setJiaofushunxu(map.getJiaofushunxu());
        inFoDtoParam.setDingdanhao(map.getDingdanhao());
        inFoDtoParam.setPeitaodanhao(map.getPeitaodanhao());
        inFoDtoParam.setShuliang(map.getShulianng());
        inFoDtoParam.setFactoryId(map.getFactoryId());
        inFoDtoParam.setHegebiaoqian(map.getHegebiaoqian());
        inFoDtoParam.setCaozuoren(loginUser.getEmpUid());
        inFoDtoParam.setType(map.getType());
        inFoDtoParam.setFathername(map.getFathername());
        inFoDtoParam.setFathernumber(map.getFathernumber());
        inFoDtoParam.setPici(map.getPici());
        inFoDtoParam.setJiaci(map.getJiaci());
        inFoDtoParam.setProportion(map.getProportion());

        mesBzzFollowService.addMesBzzFollow(inFoDtoParam);
        String objuid = map.getObjuid();
        int num1 = 0;
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getJiechunum()!=null){
                num1+=list.get(i).getJiechunum();
            }
        }
        mesBzzCpptmxService.updateCpptmxJc(objuid,num1);
        int n = mesBzzCpptmxServiceMapper.queryMesBzzCpptmxNum(map.getPtdObj());
        if (n != 0) {
            mesBzzQtfxService.updateIsItComplete1(map.getPtdObj());
        }
        JSONObject jsonObject = new JSONObject();// 需要把库存和数量数组传过去
        jsonObject.put("list",list);
        try{
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

            Client client = dcf.createClient(URL);
            Object[] objects = new Object[0];
            objects = client.invoke("getKyslJc",jsonObject.toString());
            Object arrStr = objects[0];
            String jsonObjectStr = arrStr.toString();

            AjaxResult success = AjaxResult.success();
            success.put("data", jsonObjectStr);
            return success;
        }catch (Exception e){
            e.printStackTrace();
            AjaxResult success = AjaxResult.success();
            success.put("data", "失败");
            return success;
        }
    }
}
